home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 353_01 / chap07.txt < prev    next >
Text File  |  1992-01-18  |  22KB  |  472 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.                                                         Chapter 7
  8.                                                       INHERITANCE
  9.  
  10. One reason to use inheritance is that it allows you to reuse code
  11. from a previous project but gives you the flexibility to slightly
  12. modify it if the old code doesn't do exactly what you need for the
  13. new project.  It doesn't make sense to start every new project from
  14. scratch since some code will certainly be repeated in several
  15. programs and you should strive to build on what you did previously.
  16. Moreover, it is easy to make an error if we try to modify the
  17. original class, but we are less likely to make an error if we leave
  18. the original alone and only add to it.  Another reason for using
  19. inheritance is if the project requires the use of several classes
  20. which are very similar but slightly different.
  21.  
  22. In this chapter we will concentrate on the mechanism of inheritance
  23. and how to build it into a program.  A better illustration of why
  24. you would use inheritance will be given in later chapters where we
  25. will discuss some practical applications of object oriented
  26. programming.
  27.  
  28. The principle of inheritance is available with several modern
  29. programming languages and is handled slightly differently with
  30. each.  C++ allows you to inherit all or part of the members and
  31. methods of a class, modify some, and add new ones not available in
  32. the parent class.  You have complete flexibility, and as usual,
  33. the method used with C++ has been selected to result in the most
  34. efficient code execution.
  35.  
  36.  
  37. A SIMPLE CLASS TO START WITH
  38. _________________________________________________________________
  39.  
  40. Examine the file named VEHICLE.H for a simple   =================
  41. class which we will use to begin our study of       VEHICLE.H
  42. inheritance in this chapter.  There is nothing  =================
  43. unusual about this class header, it has been
  44. kept very simple.  It consists of four simple
  45. methods which can be used to manipulate data pertaining to our
  46. vehicle.  What each method does is not especially important at this
  47. time.  We will eventually refer to this as a base class or parent
  48. class, but for the time being, we will simply use it like any other
  49. class to show that it is indeed identical to the classes already
  50. studied.  Note that we will explain the added keyword protected
  51. shortly.
  52.  
  53. Ignore lines 4, 5, and 17 until the end of this chapter where they
  54. will be explained in detail.  This file cannot be compiled or
  55. executed because it is only a header file.
  56.  
  57.  
  58.                                                          Page 7-1
  59.  
  60.                                           Chapter 7 - Inheritance
  61.  
  62.  
  63. THE IMPLEMENTATION FOR VEHICLE
  64. _________________________________________________________________
  65.  
  66. Examine the file named VEHICLE.CPP and you will   ===============
  67. find that it is the implementation of the           VEHICLE.CPP
  68. vehicle class.  The initialize() method assigns   ===============
  69. the values input as parameters to the wheels and
  70. weight variables.  We have methods to return the
  71. number of wheels and the weight, and finally, we have one that does
  72. a trivial calculation to return the loading on each wheel.  We will
  73. have a few examples of methods that do some significant processing
  74. later, but at this point, we are more interested in learning how
  75. to set up the interface to the classes, so the implementations will
  76. be kept trivial.
  77.  
  78. As stated above, this is a very simple class which will be used in
  79. the next program.  Later in this tutorial we will use it as a base
  80. class.  You should compile this class at this time in preparation
  81. for the next example program, but you cannot execute it because
  82. there is no entry point.
  83.  
  84.  
  85. USING THE VEHICLE CLASS
  86. _________________________________________________________________
  87.  
  88. The file named TRANSPRT.CPP uses the vehicle     ================
  89. class in exactly the same manner as we             TRANSPRT.CPP
  90. illustrated in the last chapter.  This should be ================
  91. an indication to you that the vehicle class is
  92. truly nothing more than a normal class as
  93. defined in C++.  We will make it a little special, however, by
  94. using it unmodified as a base class in the next few example files
  95. to illustrate inheritance.  Inheritance uses an existing class and
  96. adds functionality to it to accomplish another, possibly more
  97. complex job.
  98.  
  99. You should have no problem understanding the operation of this
  100. program.  It declares four objects of the vehicle class,
  101. initializes them, and prints out a few of the data points to
  102. illustrate that the vehicle class can be used as a simple class
  103. because it is a simple class.  We are referring to it as a simple
  104. class as opposed to calling it a base class or derived class as we
  105. will do shortly.
  106.  
  107. If you thoroughly understand this program, you should compile and
  108. execute it, remembering to link the vehicle object file with this
  109. object file.
  110.  
  111. OUR FIRST DERIVED CLASS
  112. _________________________________________________________________
  113.  
  114. Examine the file named CAR.H for our first example of the use of
  115. a derived class or child class.  The vehicle class is inherited due
  116.  
  117.                                                          Page 7-2
  118.  
  119.                                           Chapter 7 - Inheritance
  120.  
  121. to the ": public vehicle" added to line 4.  This   ==============
  122. derived class named car is composed of all of           CAR.H
  123. the information included in the base class         ==============
  124. vehicle, and all of its own additional
  125. information.  Even though we did nothing to the
  126. class named vehicle, we made it into a base class because of the
  127. way we are using it here.  To go a step further, even though it
  128. will be used as a base class in an example program later in this
  129. chapter, there is no reason it cannot continue to be used as a
  130. simple class in the previous example program.  In fact, it can be
  131. used as a single class and a base class in the same program.  The
  132. question of whether it is a simple class or a base class is
  133. answered by the way it is used.
  134.  
  135. A discussion of terminology is needed here.  When discussing object
  136. oriented programming in general, a class that inherits another is
  137. often called a derived class or a child class, but the most proper
  138. term as defined for C++ is a derived class.  Since these terms are
  139. very descriptive, and most writers tend to use the terms
  140. interchangeably, we will also use these terms in this tutorial.
  141. Likewise the proper C++ terminology for the inherited class is to
  142. call it a base class, but parent class and super class are
  143. sometimes used.
  144.  
  145. A base class is a rather general class which can cover a wide range
  146. of objects, whereas a derived class is somewhat more restricted but
  147. at the same time more useful.  For example if we had a base class
  148. named programming language and a derived class named C++, then we
  149. could use the base class to define Pascal, Ada, C++, or any other
  150. programming language, but it would not tell us about the use of
  151. classes in C++ because it can only give a general view of each
  152. language.  On the other hand, the derived class named C++ could
  153. define the use of classes, but it could not be used to describe the
  154. other languages because it is too narrow.  A base class tends to
  155. be more general, and a derived class is more specific.
  156.  
  157. In this case, the vehicle base class can be used to declare objects
  158. that represent trucks, cars, bicycles, or any number of other
  159. vehicles you can think up.  The class named car however can only
  160. be used to declare an object that is of type car because we have
  161. limited the kinds of data that can be intelligently used with it.
  162. The car class is therefore more restrictive and specific than the
  163. vehicle class.  The vehicle class is more general than the car
  164. class.
  165.  
  166. If we wished to get even more specific, we could define a derived
  167. class using car as the base class and name it sports_car and
  168. include such information as red_line_limit for the tachometer which
  169. would be silly for the family station wagon.  The car class would
  170. therefore be used as a derived class and a base class at the same
  171. time, so